﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;


namespace install_cert
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Out.WriteAsync("An unexpected error occured, please contact administrator");
                Console.In.Read();
                return;
            }

            Console.Out.WriteLine("certificate path:" + args[0]);
            if(args[0].Contains(".cer") || args[0].Contains(".pfx"))
            {
                try
                {
                    X509Certificate2 cert = new X509Certificate2(args[0]);
                    X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
                    store.Open(OpenFlags.ReadWrite);
                    store.Add(cert);
                    store.Close();
                }
                catch (Exception)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Out.WriteAsync("An unexpected error occured, please contact administrator");
                    Console.In.Read();
                }
            }
            else{
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Out.WriteAsync("Error the provided certificate is not valid");
                Console.In.Read();
            }
        }

    }
}
